//
//  OEHomeDataController.m
//  OneEightyNine
//
//  Created by apple on 2019/3/6.
//  Copyright © 2019 stoneobs. All rights reserved.
//

#import "OEHomeDataController.h"
#import "OEGoodModel.h"
@implementation OEHomeDataController
#pragma mark --首页接口
+ (void)sendGetHomeRequest:(void (^)(NSArray * _Nonnull, NSArray * _Nonnull, NSArray * _Nonnull))handle{
    
    dispatch_group_t group =  dispatch_group_create();
//    [SVProgressHUD showWithStatus:@"Waiting please"];4
    __block NSArray * bannerArray;
    __block NSArray * hotBusinessArray;
    __block NSArray * hotOfferArray;

    dispatch_group_enter(group);
    [self sendGetBannerRequestWithHandle:^(BOOL success, NSString *errorString, NSDictionary *resp) {
        NSArray * array = [OEAdverModel mj_objectArrayWithKeyValuesArray:(id)resp];
        bannerArray = array;
         dispatch_group_leave(group);
    }];
    
    dispatch_group_enter(group);
    [self sendGetBunessRequestWithHandle:^(BOOL success, NSString *errorString, NSDictionary *resp) {
        NSArray * array = [OEShopEasyModel mj_objectArrayWithKeyValuesArray:(id)resp];
        hotBusinessArray = array;
        dispatch_group_leave(group);
    }];
    
    dispatch_group_enter(group);
    [self sendGetOfferRequestWithHandle:^(BOOL success, NSString *errorString, NSDictionary *resp) {
        NSArray * array = [OEShopEasyModel mj_objectArrayWithKeyValuesArray:(id)resp];
        hotOfferArray = array;
        dispatch_group_leave(group);
    }];
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        if (handle) {
            handle(bannerArray,hotBusinessArray,hotOfferArray);
        }
        [SVProgressHUD dismiss];
    });
    
}
+ (void)sendGetBannerRequestWithHandle:(OEHomeDataNormalHandle)handle{
    NSMutableDictionary * paramDic = [NSMutableDictionary new];
    NSString * token = STUserManger.defult.loginedUser.token;
    if (token.length) {
        [paramDic setObject:token forKey:@"user_token"];
    }
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php?m=Front&c=Index&a=indexBanner"];
    DDLogInfo(@"url:%@",url);
    DDLogInfo(@"param:%@",paramDic);
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:paramDic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 NSDictionary * modelDic = [responseObject valueForKey:@"content"];
                                                 if (handle) {
                                                     handle(YES,@"",modelDic);
                                                 }
                                                 // DDLogInfo(@"json = \n%@",modelDic.mj_JSONString);
                                                 DDLogInfo(@"%@请求成功:resp\n%@",url,responseObject);
                                                 
                                                 
                                             } failure:^(NSString *stateCode, STError *error,NSError *originError) {
                                                 [SVProgressHUD dismiss];
                                                 if (handle) {
                                                     handle(NO,@"",nil);
                                                 }
                                                 DDLogError(@"\n请求失败:\nurl:%@\nparam:%@\n失败原因:%@\n错误码:%ld",
                                                            url,paramDic,error.desc,error.code);
                                             }];
}
+ (void)sendGetBunessRequestWithHandle:(OEHomeDataNormalHandle)handle{
    NSMutableDictionary * paramDic = [NSMutableDictionary new];
    NSString * token = STUserManger.defult.loginedUser.token;
    if (token.length) {
        [paramDic setObject:token forKey:@"user_token"];
    }
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php?m=Front&c=Index&a=hotRecommend"];
    DDLogInfo(@"url:%@",url);
    DDLogInfo(@"param:%@",paramDic);
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:paramDic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 NSDictionary * modelDic = [responseObject valueForKey:@"content"];
                                                 if (handle) {
                                                     handle(YES,@"",modelDic);
                                                 }
                                                 // DDLogInfo(@"json = \n%@",modelDic.mj_JSONString);
                                                 DDLogInfo(@"%@请求成功:resp\n%@",url,responseObject);
                                                 
                                                 
                                             } failure:^(NSString *stateCode, STError *error,NSError *originError) {
                                                 [SVProgressHUD dismiss];
                                                 if (handle) {
                                                     handle(NO,@"",nil);
                                                 }
                                                 DDLogError(@"\n请求失败:\nurl:%@\nparam:%@\n失败原因:%@\n错误码:%ld",
                                                            url,paramDic,error.desc,error.code);
                                             }];
}
+ (void)sendGetOfferRequestWithHandle:(OEHomeDataNormalHandle)handle{
    NSMutableDictionary * paramDic = [NSMutableDictionary new];
    NSString * token = STUserManger.defult.loginedUser.token;
    if (token.length) {
        [paramDic setObject:token forKey:@"user_token"];
    }
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php?m=Front&c=Index&a=shareSupplier"];
    DDLogInfo(@"url:%@",url);
    DDLogInfo(@"param:%@",paramDic);
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:paramDic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 NSDictionary * modelDic = [responseObject valueForKey:@"content"];
                                                 if (handle) {
                                                     handle(YES,@"",modelDic);
                                                 }
                                                 // DDLogInfo(@"json = \n%@",modelDic.mj_JSONString);
                                                 DDLogInfo(@"%@请求成功:resp\n%@",url,responseObject);
                                                 
                                                 
                                             } failure:^(NSString *stateCode, STError *error,NSError *originError) {
                                                 [SVProgressHUD dismiss];
                                                 if (handle) {
                                                     handle(NO,@"",nil);
                                                 }
                                                 DDLogError(@"\n请求失败:\nurl:%@\nparam:%@\n失败原因:%@\n错误码:%ld",
                                                            url,paramDic,error.desc,error.code);
                                             }];
}

//获取商家详情
+ (void)sendGetShopDetailRequest:(NSString*)supplier_id handle:(nonnull OEHomeDataNormalHandle)handle{
    [SVProgressHUD showWithStatus:@"Waiting"];
    NSMutableDictionary * paramDic = [NSMutableDictionary new];
    NSString * token = STUserManger.defult.loginedUser.token;
    if (token.length) {
        [paramDic setObject:token forKey:@"user_token"];
    }
    if (supplier_id.length) {
        [paramDic setObject:supplier_id forKey:@"supplier_id"];
    }
    
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php?m=Front&c=Index&a=supplierInfo"];
    DDLogInfo(@"url:%@",url);
    DDLogInfo(@"param:%@",paramDic);
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypeGet
                                          parameters:paramDic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 //                                                 [STNetWrokManger shownNormalRespMsgWithResponse:responseObject];
                                                 NSDictionary * modelDic = [responseObject valueForKey:@"content"];
                                                 if (handle) {
                                                     handle(YES,@"",modelDic);
                                                 }
                                                 DDLogInfo(@"%@请求成功:resp\n%@",url,responseObject);
                                                 
                                                 
                                             } failure:^(NSString *stateCode, STError *error,NSError *originError) {
                                                 [SVProgressHUD dismiss];
                                                 if (handle) {
                                                     handle(NO,@"",nil);
                                                 }
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                                 DDLogError(@"\n请求失败:\nurl:%@\nparam:%@\n失败原因:%@\n错误码:%ld",
                                                            url,paramDic,error.desc,error.code);
                                             }];
}
//收藏
+ (void)sendCollectShopWithSupplier_idRequest:(NSString*)supplier_id is_collect:(NSString*)is_collect  handle:(OEHomeDataNormalHandle)handle{
    [SVProgressHUD showWithStatus:@"Waiting"];
    NSMutableDictionary * paramDic = [NSMutableDictionary new];
    NSString * token = STUserManger.defult.loginedUser.token;
    if (token.length) {
        [paramDic setObject:token forKey:@"user_token"];
    }
    if (supplier_id.length) {
        [paramDic setObject:supplier_id forKey:@"supplier_id"];
    }
    if (is_collect.length) {
        [paramDic setObject:is_collect forKey:@"is_collect"];
    }
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php?m=Front&c=User&a=collectSupplier"];
    DDLogInfo(@"url:%@",url);
    DDLogInfo(@"param:%@",paramDic);
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:paramDic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 //                                                 [STNetWrokManger shownNormalRespMsgWithResponse:responseObject];
                                                 NSDictionary * modelDic = [responseObject valueForKey:@"content"];
                                                 if (handle) {
                                                     handle(YES,@"",modelDic);
                                                 }
                                                 DDLogInfo(@"%@请求成功:resp\n%@",url,responseObject);
                                                 
                                                 
                                             } failure:^(NSString *stateCode, STError *error,NSError *originError) {
                                                 [SVProgressHUD dismiss];
                                                 if (handle) {
                                                     handle(NO,@"",nil);
                                                 }
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                                 DDLogError(@"\n请求失败:\nurl:%@\nparam:%@\n失败原因:%@\n错误码:%ld",
                                                            url,paramDic,error.desc,error.code);
                                             }];
}
+ (void)sendCateListRequestHandle:(void (^)(bool, NSArray * _Nonnull))handle{
    NSMutableDictionary * paramDic = [NSMutableDictionary new];
    NSString * token = STUserManger.defult.loginedUser.token;
    if (token.length) {
        [paramDic setObject:token forKey:@"user_token"];
    }
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php?m=Front&c=Common&a=get_business_cat_list"];
    DDLogInfo(@"url:%@",url);
    DDLogInfo(@"param:%@",paramDic);
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:paramDic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 NSArray * array = [responseObject valueForKey:@"content"];
                                                 if (handle) {
                                                     handle(YES,array);
                                                 }
                                                 // DDLogInfo(@"json = \n%@",modelDic.mj_JSONString);
                                                 //                                                 [self.tableView.mj_header endRefreshing];
                                                 DDLogInfo(@"%@请求成功:resp\n%@",url,responseObject);
                                                 
                                                 
                                             } failure:^(NSString *stateCode, STError *error,NSError *originError) {
                                                 [SVProgressHUD dismiss];
                                                 ;
                                                 if (handle) {
                                                     handle(NO,nil);
                                                 }
                                                 DDLogError(@"\n请求失败:\nurl:%@\nparam:%@\n失败原因:%@\n错误码:%ld",
                                                            url,paramDic,error.desc,error.code);
                                             }];
}

//查询购物车
+ (void)sendFetchShopCarListRequestWithSupplier_id:(NSString*)supplier_id   handle:(void(^)(NSString * goods_amount,NSString * goods_price,NSArray * goods ))handle{
    NSMutableDictionary * paramDic = [NSMutableDictionary new];
    NSString * token = STUserManger.defult.loginedUser.token;
    if (token.length) {
        [paramDic setObject:token forKey:@"user_token"];
    }
    if (supplier_id.length) {
        [paramDic setObject:supplier_id forKey:@"supplier_id"];
    }
    [paramDic setObject:NSBundle.st_UUID forKey:@"unique_id"];
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php?m=Front&c=Cart&a=index"];
    DDLogInfo(@"url:%@",url);
    DDLogInfo(@"param:%@",paramDic);
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:paramDic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
//                                                 [SVProgressHUD dismiss];
                                                 NSDictionary * modelDic = responseObject[@"content"];
                                                 NSArray * array = modelDic[@"goods_list"];
                                                 NSArray * goodArray = [OEGoodModel mj_objectArrayWithKeyValuesArray:array];
                                                 NSString *goods_price = [modelDic[@"total"][@"goods_price"] description];
                                                 NSString *cart_number = [modelDic [@"total"][@"cart_number"] description];
                                                 if (handle) {
                                                     handle(cart_number,goods_price,goodArray);
                                                 }
                                                 // DDLogInfo(@"json = \n%@",modelDic.mj_JSONString);
                                                 //                                                 [self.tableView.mj_header endRefreshing];
                                                 DDLogInfo(@"%@请求成功:resp\n%@",url,responseObject);
                                                 
                                                 
                                             } failure:^(NSString *stateCode, STError *error,NSError *originError) {
//                                                 [SVProgressHUD dismiss];
                                                 if (handle) {
                                                     handle(@"0",@"0",@[]);
                                                 }
                                                 DDLogError(@"\n请求失败:\nurl:%@\nparam:%@\n失败原因:%@\n错误码:%ld",
                                                            url,paramDic,error.desc,error.code);
                                             }];
}

//添加至购物车
+ (void)addGoodToCar:(NSString*)goods_id handle:(OEHomeDataNormalHandle)handle{
    
    NSMutableDictionary * paramDic = [NSMutableDictionary new];
    NSString * token = STUserManger.defult.loginedUser.token;
    if (token.length) {
        [paramDic setObject:token forKey:@"user_token"];
    }
    if (goods_id.length) {
        [paramDic setObject:goods_id forKey:@"goods_id"];
    }
    [paramDic setObject:@"1" forKey:@"number"];
    [paramDic setObject:NSBundle.st_UUID forKey:@"unique_id"];
    
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php?m=Front&c=Cart&a=add_to_cart"];
    DDLogInfo(@"url:%@",url);
    DDLogInfo(@"param:%@",paramDic);
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:paramDic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
//                                                 [SVProgressHUD dismiss];
                                                 NSDictionary * modelDic = [responseObject valueForKey:@"content"];
                                                 if (handle) {
                                                     handle(YES,@"",modelDic);
                                                 }
                                                 // DDLogInfo(@"json = \n%@",modelDic.mj_JSONString);
                                                 DDLogInfo(@"%@请求成功:resp\n%@",url,responseObject);
                                                 
                                                 
                                             } failure:^(NSString *stateCode, STError *error,NSError *originError) {
//                                                 [SVProgressHUD dismiss];
                                                 if (handle) {
                                                     handle(YES,@"",nil);
                                                 }
                                                 DDLogError(@"\n请求失败:\nurl:%@\nparam:%@\n失败原因:%@\n错误码:%ld",
                                                            url,paramDic,error.desc,error.code);
                                             }];
}
//减少购物车 商品数量
+ (void)sendReduceGoodToCar:(NSString*)goods_id number:(NSString*)numer handle:(OEHomeDataNormalHandle)handle{
    NSMutableDictionary * paramDic = [NSMutableDictionary new];
    NSString * token = STUserManger.defult.loginedUser.token;
    if (token.length) {
        [paramDic setObject:token forKey:@"user_token"];
    }
    if (goods_id.length) {
        [paramDic setObject:goods_id forKey:@"goods_id"];
    }
    if (numer.length) {
        [paramDic setObject:numer forKey:@"number"];
    }
//    [paramDic setObject:@"1" forKey:@"number"];
//    [paramDic setObject:NSBundle.st_UUID forKey:@"unique_id"];
    
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php?m=Front&c=Cart&a=reduceGoods"];
    DDLogInfo(@"url:%@",url);
    DDLogInfo(@"param:%@",paramDic);
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:paramDic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 //                                                 [SVProgressHUD dismiss];
                                                 NSDictionary * modelDic = [responseObject valueForKey:@"content"];
                                                 if (handle) {
                                                     handle(YES,@"",modelDic);
                                                 }
                                                 // DDLogInfo(@"json = \n%@",modelDic.mj_JSONString);
                                                 DDLogInfo(@"%@请求成功:resp\n%@",url,responseObject);
                                                 
                                                 
                                             } failure:^(NSString *stateCode, STError *error,NSError *originError) {
                                                 //                                                 [SVProgressHUD dismiss];
                                                 if (handle) {
                                                     handle(YES,@"",nil);
                                                 }
                                                 DDLogError(@"\n请求失败:\nurl:%@\nparam:%@\n失败原因:%@\n错误码:%ld",
                                                            url,paramDic,error.desc,error.code);
                                             }];
}

//删出购物车 商品数量
+ (void)sendDeleteShopCarRequestWithRec_id:(NSString*)rec_id  handle:(OEHomeDataNormalHandle)handle{
    NSMutableDictionary * paramDic = [NSMutableDictionary new];
    NSString * token = STUserManger.defult.loginedUser.token;
    if (token.length) {
        [paramDic setObject:token forKey:@"user_token"];
    }
    if (rec_id.length) {
        [paramDic setObject:rec_id forKey:@"rec_id"];
    }
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php?m=Front&c=Cart&a=cartDel"];
    DDLogInfo(@"url:%@",url);
    DDLogInfo(@"param:%@",paramDic);
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:paramDic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 //                                                 [SVProgressHUD dismiss];
                                                 NSDictionary * modelDic = [responseObject valueForKey:@"content"];
                                                 if (handle) {
                                                     handle(YES,@"",modelDic);
                                                 }
                                                 // DDLogInfo(@"json = \n%@",modelDic.mj_JSONString);
                                                 DDLogInfo(@"%@请求成功:resp\n%@",url,responseObject);
                                                 
                                                 
                                             } failure:^(NSString *stateCode, STError *error,NSError *originError) {
                                                 //                                                 [SVProgressHUD dismiss];
                                                 if (handle) {
                                                     handle(YES,@"",nil);
                                                 }
                                                 DDLogError(@"\n请求失败:\nurl:%@\nparam:%@\n失败原因:%@\n错误码:%ld",
                                                            url,paramDic,error.desc,error.code);
                                             }];
}
//添加订单
+ (void)sendCommitOrderRequestWithRec_ids:(NSString *)rec_ids supplier_id:(NSString *)supplier_id handle:(OEHomeDataNormalHandle)handle{
    NSMutableDictionary * paramDic = [NSMutableDictionary new];
    NSString * token = STUserManger.defult.loginedUser.token;
    if (token.length) {
        [paramDic setObject:token forKey:@"user_token"];
    }
    if (rec_ids.length) {
        [paramDic setObject:rec_ids forKey:@"rec_ids"];
    }
    if (supplier_id.length) {
        [paramDic setObject:supplier_id forKey:@"supplier_id"];
    }
    [paramDic setObject:NSBundle.st_UUID forKey:@"unique_id"];
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php?m=Front&c=Cart&a=done"];
    DDLogInfo(@"url:%@",url);
    DDLogInfo(@"param:%@",paramDic);
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:paramDic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 //                                                 [SVProgressHUD dismiss];
                                                 NSDictionary * modelDic = [responseObject valueForKey:@"content"];
                                                 if (handle) {
                                                     handle(YES,@"",modelDic);
                                                 }
                                                 // DDLogInfo(@"json = \n%@",modelDic.mj_JSONString);
                                                 DDLogInfo(@"%@请求成功:resp\n%@",url,responseObject);
                                                 
                                                 
                                             } failure:^(NSString *stateCode, STError *error,NSError *originError) {
                                                 //                                                 [SVProgressHUD dismiss];
                                                 if (handle) {
                                                     handle(YES,@"",nil);
                                                 }
                                                 DDLogError(@"\n请求失败:\nurl:%@\nparam:%@\n失败原因:%@\n错误码:%ld",
                                                            url,paramDic,error.desc,error.code);
                                             }];
}
@end
